package cn.tedu.charging.order.service.impl;


import cn.tedu.charging.common.consts.GunStatus;
import cn.tedu.charging.common.consts.TopicNameStatus;
import cn.tedu.charging.common.pojo.param.GunStatusUpdateParam;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.common.pojo.vo.UserInfoVO;
import cn.tedu.charging.common.result.JsonResult;
import cn.tedu.charging.common.utils.JsonUtil;
import cn.tedu.charging.order.dao.mapper.ChargingBillSuccessMapper;
import cn.tedu.charging.order.feign.DeviceClient;
import cn.tedu.charging.order.feign.UserClient;
import cn.tedu.charging.order.mqtt.MqttProducter;
import cn.tedu.charging.order.pojo.dto.ChargingDto;
import cn.tedu.charging.order.pojo.parame.OrderAddParam;
import cn.tedu.charging.order.pojo.po.RabbitmqDeadPo;
import cn.tedu.charging.order.quartz.DeviceCheckJob;
import cn.tedu.charging.order.rabbitmq.RabbitMQProducter;
import cn.tedu.charging.order.service.OrderService;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;


@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ChargingBillSuccessMapper chargingBillSuccessMapper;


    @Autowired
    private UserClient userClient;
    @Autowired
    private DeviceClient deviceClient;

    @Autowired
    private MqttProducter mqttProducter;

    @Autowired
    private RabbitMQProducter rabbitMQProducter;


    @Override
    public void addOrder(OrderAddParam orderAddParam) {
        String orderNo = getOrderNo();
        updateGunStatus(GunStatus.Gun_Status_Busy);
        // 发送开始充电信息
        sendChargingStart(orderAddParam);

        log.debug("设备准备开始自检 倒计时 1分钟 等待设备响应 ");
        try {
            DeviceCheckJob deviceCheckJob = new DeviceCheckJob(orderNo,orderAddParam.getGunId());
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }

    }

    private String getOrderNo() {
        String start = "10000";
        Random random = new Random();
        int r = random.nextInt(1000);
        long now = System.currentTimeMillis();
        String orderNo = start + "_" + r  + "_" + now;
        return orderNo;
    }

    private void sendChargingStart(OrderAddParam orderAddParam) {
        String topic = "topic"+TopicNameStatus.CHARGING_START+orderAddParam.getPileId();
        ChargingDto chargingDto=new ChargingDto();
        BeanUtils.copyProperties(orderAddParam,chargingDto);
        chargingDto.setOrderNo(666666666666L);
        chargingDto.setMessage(TopicNameStatus.CHARGING_START);
        String jsonMessage = JsonUtil.toJson(chargingDto);
        mqttProducter.sendMqtt(topic, jsonMessage);
    }

    private Boolean updateGunStatus(Integer gunStatusBusy) {
        GunStatusUpdateParam gunStatusUpdateParam = new GunStatusUpdateParam();
        gunStatusUpdateParam.setGunId(gunStatusBusy);
        JsonResult<Boolean> booleanJsonResult = deviceClient.updateGunStatus(gunStatusUpdateParam);
        if (booleanJsonResult != null){
            return booleanJsonResult.getData();
        }
        return false;
    }

    private StationInfoVO getStationInfo(Integer userId) {
        JsonResult<StationInfoVO> stationInfo = deviceClient.getStationInfo(userId);
        if (stationInfo != null){
            return stationInfo.getData();
        }
        return null;
    }

    private UserInfoVO getUserInfo(Integer userId) {
        JsonResult<UserInfoVO> userInfo = userClient.getUserInfo(userId);
        if (userInfo != null){
            return userInfo.getData();
        }
        return null;
    }
}
